package cn.xz.xy.activity


import android.os.Handler
import android.util.Log
import android.widget.SeekBar
import androidx.recyclerview.widget.DefaultItemAnimator
import androidx.recyclerview.widget.LinearLayoutManager
import cn.xz.xy.R
import cn.xz.xy.adapter.SeListAdapter
import cn.xz.xy.base.BaseActivity
import cn.xz.xy.base.BaseApplication
import cn.xz.xy.bean.CommonLib
import cn.xz.xy.bean.DeviceList
import cn.xz.xy.bean.U3dListBean
import cn.xz.xy.databinding.ActivitySecondBinding
import cn.xz.xy.dialog.LocalListDialog
import cn.xz.xy.net.ControlDevice
import cn.xz.xy.net.HttpEvent
import cn.xz.xy.util.*

class SecondActivity : BaseActivity<ActivitySecondBinding>(R.layout.activity_second) {

    //设备信息
    private var mDevice: DeviceList? = null

    //当前选择的主机
    private var mDNum = 0

    //当前视频进度值
    private var pMove = 0
    private var handler: Handler? = null

    private val us3List = intArrayOf(14034,14035,14033,14046,14050,14051)

    //获取数据通用回调，因为请求异步，加回调判断
    interface OnMListener {
        fun getData(list: ArrayList<GetListUtils.TestData>) //有参方法
    }
    override fun initData() {
        //获取跳转数据
        val mTag = intent.getIntExtra(CommonLib.JUMP_TYPE, -1)
        //标题
        var mTitle = ""
        //如果是多级列表
        //设置层级
        setList()
        mDataBind.tvZj.gone()
        mDataBind.tvTy.gone()
        //根据参数进行二次分类
        val mFirstList: ArrayList<GetListUtils.TestData>  = arrayListOf()
        when(mTag){
            111->{ //茶饮
                mTitle = "茶饮-品茶与人文"
                mFirstList.add(GetListUtils.TestData("茶话悠长",14034)) //3d
                mFirstList.add(GetListUtils.TestData("鉴水”水为茶之母“",14035)) //3d
                mFirstList.add(GetListUtils.TestData("秦巴茶艺",14036))
                mFirstList.add(GetListUtils.TestData("茶馆风情",14037))
                mFirstList.add(GetListUtils.TestData("茶与婚礼",14038))
                mFirstList.add(GetListUtils.TestData("茶与诗意",14039))
                mFirstList.add(GetListUtils.TestData("抒写茶意",14040))
            }
            222->{ //茶源
                mTitle = "茶源-起源与历史"
                mFirstList.add(GetListUtils.TestData("茶树的形态特征",14033)) //3d
            }
            333->{ //茶业
                mTitle = "茶业-加工与产业"
                mFirstList.add(GetListUtils.TestData("手工炒茶互动",14044))
                mFirstList.add(GetListUtils.TestData("手工炒茶技艺",14045))
                mFirstList.add(GetListUtils.TestData("茶业审评",14046)) //3d
                mFirstList.add(GetListUtils.TestData("茶叶加工",14047))
                mFirstList.add(GetListUtils.TestData("茶与健康",14048))
                mFirstList.add(GetListUtils.TestData("茶叶百科",14049))
                mFirstList.add(GetListUtils.TestData("环境与茶",14050)) //3d
                mFirstList.add(GetListUtils.TestData("茶企茶叶产品展示",14051)) //3d
                mFirstList.add(GetListUtils.TestData("幸福茶乡",14052))

                mDataBind.tvZj.visible()
                mDataBind.tvTy.visible()
            }
            444->{ //序厅
                mTitle = "序厅"
                mFirstList.add(GetListUtils.TestData("联动沙盘",14032))
            }
        }
        mDataBind.tvTitle.text = mTitle
        //设置一级目录
        GetListUtils.setListData(1, mFirstList, object : GetListUtils.OnListener {
            override fun onClick(l: GetListUtils.TestData) {
                mDNum = l.Tag!!
                mDevice = ControlDevice.getThisDevice(mDNum)
                //mDevice!!.ip = "192.168.140.237"
                //获取当前音量
                getVoice()
                //开启定时器获取视频进度
                handler = Handler()
                handler?.post(movieR)
                //设置本地列表
                initRv()
                getLocalList()
                setList2(l.Tag!!)
            }
        })
    }

    var mAdapter: SeListAdapter?  = null
    private fun initRv(){
        if(mAdapter == null){
            mDataBind.rvLocalList.layoutManager = LinearLayoutManager(BaseApplication.getContext())
            mDataBind.rvLocalList.itemAnimator = DefaultItemAnimator()
            mAdapter = SeListAdapter(mDevice!!)
            mDataBind.rvLocalList.adapter = mAdapter
        }
    }

    override fun initEvent() {
        //返回
        mDataBind.tvBack.setOnClickListener {
            finish()
        }
        //播放
        mDataBind.zkPlay.setOnClickListener {
            if (mDevice == null){
                return@setOnClickListener
            }
            if (is3D()){
                U3dHttp.PlayMovie(mDevice!!.ip)
            }else{
                LocalHttp.moviePlay(mDevice!!.ip,1)
            }
        }
        //暂停
        mDataBind.zkPause.setOnClickListener {
            if (mDevice == null){
                return@setOnClickListener
            }
            if (is3D()){
                U3dHttp.PauseMovie(mDevice!!.ip)
            }else{
                LocalHttp.moviePlay(mDevice!!.ip,2)
            }
        }
        //重播
        mDataBind.zkReplay.setOnClickListener {
            if (mDevice == null){
                return@setOnClickListener
            }
            if (is3D()){
                U3dHttp.ReplayMovie(mDevice!!.ip)
            }else{
                LocalHttp.moviePlay(mDevice!!.ip,3)
            }
        }
        //上一页
        mDataBind.zkPageUp.setOnClickListener {
            if (mDevice == null){
                return@setOnClickListener
            }
            if (is3D()){
                U3dHttp.PrevPage(mDevice!!.ip)
            }else{
                LocalHttp.moviePlay(mDevice!!.ip,9)
            }
        }
        //下一页
        mDataBind.zkPageDown.setOnClickListener {
            if (mDevice == null){
                return@setOnClickListener
            }
            if (is3D()){
                U3dHttp.NextPage(mDevice!!.ip)
            }else{
                LocalHttp.moviePlay(mDevice!!.ip,10)
            }
        }
        //声音滑动监听
        mDataBind.zkVolumeSeekbar.setOnSeekBarChangeListener(object :
            SeekBar.OnSeekBarChangeListener {
            override fun onProgressChanged(seekBar: SeekBar, progress: Int, fromUser: Boolean) {
                if (mDevice != null){
                    SeekBarPopUtils.move(progress, seekBar, false)
                    LocalHttp.changeVoice(mDevice!!.ip,progress)
                }
            }

            override fun onStartTrackingTouch(seekBar: SeekBar) {
                if (mDevice != null){
                    SeekBarPopUtils.showPop(seekBar)
                }
            }

            override fun onStopTrackingTouch(seekBar: SeekBar) {
                if (mDevice != null){
                    SeekBarPopUtils.dismiss()
                }
            }
        })
        //视频滑动监听
        mDataBind.zkVideoSeekbar.setOnSeekBarChangeListener(object :
            SeekBar.OnSeekBarChangeListener {
            override fun onProgressChanged(seekBar: SeekBar, progress: Int, fromUser: Boolean) {
                if (mDevice != null){
                    SeekBarPopUtils.move(progress, seekBar, true)
                    pMove = progress
                }
            }

            override fun onStartTrackingTouch(seekBar: SeekBar) {
                if (mDevice != null){
                    SeekBarPopUtils.showPop(seekBar)
                }
            }

            override fun onStopTrackingTouch(seekBar: SeekBar) {
                if (mDevice != null){
                    SeekBarPopUtils.dismiss()
                    if (is3D()){
                        U3dHttp.changeMovie(mDevice!!.ip,pMove)
                    }else{
                        LocalHttp.changeMovie(mDevice!!.ip,pMove)
                    }
                }
            }
        })
        //主机信号 2341
        mDataBind.tvZj.setOnClickListener {
            val hdDevice = ControlDevice.getHDDevice(2341);
            if (hdDevice != null){
                SendTcpUtils.setTcp(
                    ZhuanHuan.hexStringToBytes(hdDevice.openProtocol),
                    hdDevice.ip,
                    hdDevice.port)
            }
        }
        //投影信号 2342
        mDataBind.tvTy.setOnClickListener {
            val hdDevice = ControlDevice.getHDDevice(2342);
            if (hdDevice != null){
                SendTcpUtils.setTcp(
                    ZhuanHuan.hexStringToBytes(hdDevice.openProtocol),
                    hdDevice.ip,
                    hdDevice.port)
            }
        }

    }


    //获取本地资源列表
    private fun getLocalList(){
        if (!is3D()){
            mDataBind.rvLocalList.visible()
            LocalHttp.getPlayData(this,mDevice!!.ip,LocalHttp.GetPlayDataListener {
                if (it.isNotEmpty() && mAdapter != null){
                    mAdapter!!.setList(it)

                }
            })
        }else{
            mDataBind.rvLocalList.gone()
        }
    }


    //判断是否是3d
    private fun is3D(): Boolean{
        var get = false
        us3List.forEach {
            if (it == mDNum){
                get = true;
            }
        }
        return get
    }

    private fun setList() {
        //这个集合存储的是每一层级的样式以及信息，会根据该集合的长度去创建最大多少级
        val mList: ArrayList<GetListUtils.GetListData> = ArrayList<GetListUtils.GetListData>()
        mList.add(GetListUtils.GetListData(itemSize = 15f, leftMargin = 20))
        mList.add(GetListUtils.GetListData(itemSize = 14f))
        mList.add(GetListUtils.GetListData(itemSize = 14f))
        GetListUtils.find(mDataBind.clAll, this@SecondActivity, mList)
    }

    //二级列表
    private fun setList2(tag: Int) {
        getListContext(tag,true,object :OnMListener{
            override fun getData(list: ArrayList<GetListUtils.TestData>) {
                if (list.size > 0) {
                    Log.e("TAG", "setListData--->: 这是栏目")
                } else {
                    Log.e("TAG", "setListData--->: 这是内容")
                }
                GetListUtils.setListData(2, list, object : GetListUtils.OnListener {
                    override fun onClick(l: GetListUtils.TestData) {

                        getListContext(l.Tag!!,false,object :OnMListener{
                            override fun getData(list: ArrayList<GetListUtils.TestData>) {
                                if (list.size > 0) {
                                    Log.e("TAG", "setListData--->: 这是栏目")
                                } else {
                                    Log.e("TAG", "setListData--->: 这是内容")
                                    U3dHttp.PostChannel(mDevice!!.ip,l.Tag!!)
                                }
                                GetListUtils.setListData(3, list, object : GetListUtils.OnListener {
                                    override fun onClick(l: GetListUtils.TestData) {
                                        getListContext(l.Tag!!,false,object :OnMListener{
                                            override fun getData(list: ArrayList<GetListUtils.TestData>) {
                                                if (list.size > 0) {
                                                    Log.e("TAG", "setListData--->: 这是栏目")
                                                } else {
                                                    Log.e("TAG", "setListData--->: 这是内容")
                                                    U3dHttp.PostContent(mDevice!!.ip,l.Tag!!)
                                                }
                                            }
                                        })
                                    }
                                })
                            }
                        })
                    }
                })
            }
        })
    }

    //根据tag返回对应的集合
    private fun getListContext(tag: Int,isGetParent: Boolean,mListener: OnMListener){
        //如果是3u获取列表，其他的直接控客户端
        if (is3D()){
            //获取列表
            getUnitData(tag, isGetParent, mListener)
        }else{

        }
    }

    private fun getVoice(){
        LocalHttp.getVoiceSize(mDevice!!.ip,LocalHttp.GetVoiceSizeListener {
            mDataBind.zkVolumeSeekbar.post {
                mDataBind.zkVolumeSeekbar.progress = it
            }
        })
    }

    private var movieR: Runnable = object : Runnable {
        override fun run() {
            getMove()
            handler?.postDelayed(this, 1000)
        }
    }

    private fun getMove(){
        if (is3D()){
            //获取程序的
            U3dHttp.getMovieSize(mDevice!!.ip,U3dHttp.GetMovieSizeListener { maxTxt, minTxt, maxSeekBar, seekProgress ->
                mDataBind.zkVideoSeekbar.post {
                    mDataBind.zkMaxTime.text = maxTxt
                    mDataBind.zkMinTime.text = minTxt
                    mDataBind.zkVideoSeekbar.max = maxSeekBar
                    mDataBind.zkVideoSeekbar.progress = seekProgress
                }
            })
        }else{
            //客户端的
            LocalHttp.getMovieSize(mDevice!!.ip,LocalHttp.GetMovieSizeListener { maxTxt, minTxt, maxSeekBar, seekProgress ->
                mDataBind.zkVideoSeekbar.post {
                    mDataBind.zkMaxTime.text = maxTxt
                    mDataBind.zkMinTime.text = minTxt
                    mDataBind.zkVideoSeekbar.max = maxSeekBar
                    mDataBind.zkVideoSeekbar.progress = seekProgress
                }
            })
        }
    }

    //请求获取Unit数据
    private fun getUnitData(tag: Int, isGetParent: Boolean, mListener: OnMListener) {
        U3dHttp.getU3dData(this,mDevice!!.ip
        ) { mBean ->
            if (mBean != null) {
                setMList(tag, mBean, isGetParent, mListener)
            }else{
                mListener.getData(arrayListOf())
            }
        }
    }

    private fun setMList(tag: Int, response: U3dListBean, isGetParent: Boolean, mListener: OnMListener) {
        var mPTag = tag
        val bhList: List<U3dListBean.BhList> = response.list
        val subColumns: List<U3dListBean.SubColumns> = response.subColumns
        if (isGetParent) {
            //筛选出父类
            for (b in subColumns) {
                if (0 == b.parentTag && canGet(b.name)) {
                    mPTag = b.tag
                }
            }
        }

        //筛选出对应的二级数据
        val mSXList: ArrayList<GetListUtils.TestData> = ArrayList<GetListUtils.TestData>()
        for (b in subColumns) {
            if (mPTag == b.parentTag && canGet(b.name)) {
                mSXList.add(GetListUtils.TestData(b.name, b.tag, b.parentTag))
            }
        }

        if (mSXList.isEmpty()) { //无数据，从list里面筛选
            for (h in bhList) {
                if (h.tag == mPTag && canGet(h.title)) {
                    mSXList.add( GetListUtils.TestData(h.title, h.id, h.tag))
                }
            }
        }

        mListener.getData(mSXList)
        //如果此时总数据还为0,则将list全部添加进去
        /*if (mSXList.size() == 0) {
            if (subColumns.size() == 0) {
                for (BhListBean.BhList h : bhList) {
                    mSXList.add(setData(h.getTitle(), h.getTag(), h.getID()));
                }
            }
        }*/
    }


    //动态筛选不需要的字符串
    private fun canGet(s: String): Boolean {
        return s !== "null" && !s.contains(".png") && !s.contains(".jpg")
    }

    override fun onDestroy() {
        super.onDestroy()
        //及时置空
        if (handler != null){
            handler?.removeCallbacks(movieR)
            handler = null
        }
    }
}